﻿namespace NEclipse
{
  using System;
  using System.Collections.Generic;
  using System.Windows.Forms;
  using FlyingTabsLib;
  using NEclipse.CoreLib;
  using NEclipse.CoreLib.Workspace;
  using NEclipse.Localization;

  /// <summary>
  /// Implementation of the main window.
  /// </summary>
  public partial class MainWindow : Form
  {
    /// <summary>
    /// A list of all already loaded vies.
    /// </summary>
    private Dictionary<Type, IView> loadedViews = new Dictionary<Type, IView>();

    /// <summary>
    /// The used workspace manager.
    /// </summary>
    private WorkspaceManager workspaceManger;

    /// <summary>
    /// Initializes a new instance of the <see cref="MainWindow"/> class.
    /// </summary>
    public MainWindow()
    {
      this.InitializeComponent();
      this.tabControl1.EnableDragAndDropTabs();
      this.tabControl2.EnableDragAndDropTabs();
      this.tabControl3.EnableDragAndDropTabs();
    }

    /// <summary>
    /// Handles the Load event of the MainWindow control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void MainWindow_Load(object sender, EventArgs e)
    {
      this.Hide();
      WorkspaceLauncher workspaceLauncher = new WorkspaceLauncher();
      if (workspaceLauncher.ShowDialog() == DialogResult.OK)
      {
        this.workspaceManger = new WorkspaceManager(workspaceLauncher.WorkspaceDirectory);
        workspaceLauncher.Dispose();
        PluginLoader loader = new PluginLoader();
        loader.LoadPlugins(@"C:\Projekte\NEclipse\trunk\Implementation\NEclipse");

        foreach (Type t in loader.Perspectives)
        {
          ToolStripMenuItem tsmi = new ToolStripMenuItem(t.Name);
          tsmi.Tag = t;
          tsmi.Click += new EventHandler(this.Perspective_Click);
          openPerspectiveToolStripMenuItem.DropDownItems.Insert(0, tsmi);
        }

        foreach (Type t in loader.Views)
        {
          ToolStripMenuItem tsmi = new ToolStripMenuItem(t.Name);
          tsmi.Tag = t;
          tsmi.Click += new EventHandler(this.View_Click);
          this.showViewToolStripMenuItem.DropDownItems.Insert(0, tsmi);
        }

        this.Show();
      }
      else
      {
        workspaceLauncher.Dispose();
        Application.Exit();
      }
    }

    /// <summary>
    /// Handles the Click event of the Perspective menu items.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void Perspective_Click(object sender, EventArgs e)
    {
      IPerspective perspective = (IPerspective)Activator.CreateInstance((sender as ToolStripMenuItem).Tag as Type);
      this.tabControl1.TabPages.Clear();
      foreach (Type t in perspective.LeftTabs)
      {
        IView view;
        if (this.loadedViews.ContainsKey(t) == false)
        {
          view = (IView)Activator.CreateInstance(t);
          view.Initialize(this.workspaceManger);
        }
        else
        {
          view = this.loadedViews[t];
        }

        TabPage tabPage = new TabPage(view.Name);
        view.ViewUserControl.Dock = DockStyle.Fill;
        tabPage.Controls.Add(view.ViewUserControl);
        this.tabControl1.TabPages.Add(tabPage);
      }
    }

    /// <summary>
    /// Handles the Click event of the View menu items.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void View_Click(object sender, EventArgs e)
    {
    }

    /// <summary>
    /// Handles the Click event of the exitToolStripMenuItem control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.CloseApplication();
    }

    /// <summary>
    /// Handles the FormClosing event of the MainWindow control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
    private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (e.CloseReason == CloseReason.UserClosing)
      {
        this.CloseApplication();

        // There was a reason not to close the application, cancel closing
        e.Cancel = true;
      }
    }

    /// <summary>
    /// Closes the application.
    /// </summary>
    private void CloseApplication()
    {
      if (MessageBox.Show(MessageBoxQuestions.Text_ExitApplication, MessageBoxQuestions.Title_ExitApplication, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        this.workspaceManger.SaveConfiguration();
        Application.Exit();
      }
    }
  }
}
